001    /*
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: 2002-9-6
005     * Time: 15:53:41
006     * To change template for new class use 
007     * Code Style | Class Templates options (Tools | IDE Options).
008     */
009    package EVolve.util.overlappers;
010    
011    import EVolve.visualization.*;
012    import EVolve.visualization.VizFactory.VisualizationFactory;
013    import EVolve.visualization.XYViz.XYVisualization;
014    import EVolve.Scene;
015    import EVolve.util.predefinedutils.*;
016    import EVolve.util.unifyutils.Unification;
017    import EVolve.exceptions.NoDataPlotException;
018    import EVolve.exceptions.EVolveException;
019    
020    import javax.swing.*;
021    import javax.swing.filechooser.FileSystemView;
022    import java.awt.*;
023    import java.awt.event.*;
024    import java.io.*;
025    import java.util.*;
026    
027    public class AsynchronousOverlapper extends OverlapVisualization {
028        private final String name = "AsynchronousOverlapper";
029        private JList fileList,procList;
030        private JTextField txtConfName;
031        private HashSet setFile;
032        private ArrayList setProcessFiles;
033        private DefaultListModel procListModel,fileListModel;
034        private ArrayList[] entityList;
035        private ArrayList[] referenceDimList;
036    
037        public AsynchronousOverlapper() {
038            super();
039            dialog = null;
040            setProcessFiles = new ArrayList();
041            setFile = new HashSet();
042            colorList = new ArrayList();
043            entityList = new ArrayList[2];
044            entityList[0] = new ArrayList();
045            entityList[1] = new ArrayList();
046            referenceDimList = new ArrayList[2];
047            referenceDimList[0] = new ArrayList();
048            referenceDimList[1] = new ArrayList();
049        }
050    
051        public String getName() {
052            return name;
053        }
054    
055        public void createDialog() {
056            setFile.clear();
057            setProcessFiles.clear();
058            colorList.clear();
059            visualizationList.clear();
060            dialog = new JDialog(Scene.getFrame(),"Overlap Visualizations...",true);
061            dialog.setBounds(new Rectangle(500,400));
062    
063            JPanel batchName = new JPanel(new FlowLayout());
064            dialog.getContentPane().add(batchName,BorderLayout.NORTH);
065    
066            Box boxMain = new Box(BoxLayout.Y_AXIS);
067            boxMain.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
068                    "Choose data source(s) to be overlapped & Processing configuration"));
069            dialog.getContentPane().add(boxMain,BorderLayout.CENTER);
070    
071    
072            Box boxLabels = new Box(BoxLayout.X_AXIS);
073            JButton buttonGetDir = new JButton("Select Directory ..");
074            buttonGetDir.addActionListener(new ActionListener(){
075                public void actionPerformed(ActionEvent e){
076                        fillFileList();
077                }
078            });
079            boxLabels.add(buttonGetDir);
080            boxLabels.add(Box.createHorizontalGlue());
081            boxLabels.add(new JLabel("Processing List                             "));
082    
083            Box boxLists = new Box(BoxLayout.X_AXIS);
084            fileListModel = new DefaultListModel();
085            fileList = new JList(fileListModel);
086    
087            Box boxAddRemove = new Box(BoxLayout.Y_AXIS);
088            JButton buttonAdd = new JButton("  >  ");
089            buttonAdd.addActionListener(new ActionListener(){
090                public void actionPerformed(ActionEvent e){
091                    addFiles();
092                }
093            });
094            JButton buttonRemove = new JButton("  <  ");
095            buttonRemove.addActionListener(new ActionListener(){
096                public void actionPerformed(ActionEvent e){
097                    removeFiles();
098                }
099            });
100            boxAddRemove.add(Box.createVerticalStrut(40));
101            boxAddRemove.add(buttonAdd);
102            boxAddRemove.add(Box.createVerticalStrut(20));
103            boxAddRemove.add(buttonRemove);
104    
105            procListModel = new DefaultListModel();
106            procList = new JList(procListModel);
107    
108            JScrollPane scrollPane1 = new JScrollPane(fileList);
109            JScrollPane scrollPane2 = new JScrollPane(procList);
110            boxLists.add(scrollPane1);
111            boxLists.add(boxAddRemove);
112            boxLists.add(scrollPane2);
113    
114            boxMain.add(boxLabels);
115            boxMain.add(Box.createVerticalStrut(5));
116            boxMain.add(boxLists);
117    
118            Box boxConfig = new Box(BoxLayout.X_AXIS);
119            boxConfig.add(new JLabel("Choose configure file:"));
120            txtConfName = new JTextField(12);
121            boxConfig.add(Box.createHorizontalStrut(20));
122            boxConfig.add(txtConfName,BorderLayout.CENTER);
123            JButton buttonConfig = new JButton("...");
124            buttonConfig.addActionListener(new ActionListener(){
125                public void actionPerformed(ActionEvent e){
126                    txtConfName.setText(chooseConfig());
127                }
128            });
129            boxConfig.add(buttonConfig,BorderLayout.EAST);
130    
131            Box boxOkCancel = new Box(BoxLayout.X_AXIS);
132            JButton buttonOK = new JButton("OK");
133            JButton buttonCancel = new JButton("Cancel");
134            JButton buttonColor = new JButton("Coloring");
135    
136            buttonOK.addActionListener(new ActionListener(){
137                public void actionPerformed(ActionEvent e){
138                    onOK();
139                }
140            });
141            buttonCancel.addActionListener(new ActionListener(){
142                public void actionPerformed(ActionEvent e){
143                    onCancel();
144                }
145            });
146            buttonColor.addActionListener(new ActionListener(){
147                public void actionPerformed(ActionEvent e){
148                    selectColor();
149                }
150            });
151    
152            boxOkCancel.add(Box.createHorizontalStrut(25));
153            boxOkCancel.add(buttonColor);
154            boxOkCancel.add(Box.createHorizontalStrut(20));
155            boxOkCancel.add(buttonOK);
156            boxOkCancel.add(Box.createHorizontalStrut(20));
157            boxOkCancel.add(buttonCancel);
158    
159            Box boxBottom = Box.createVerticalBox();
160            boxBottom.add(Box.createVerticalStrut(12));
161            boxBottom.add(boxConfig);
162            boxBottom.add(Box.createVerticalStrut(30));
163            boxBottom.add(boxOkCancel);
164    
165    
166            dialog.getContentPane().add(boxBottom,BorderLayout.SOUTH);
167            //dialog.setResizable(false);
168    
169        }
170    
171        private void selectColor() {
172            int index = procList.getSelectedIndex();
173    
174            if (index != -1) {
175                Color newColor = JColorChooser.showDialog(Scene.getFrame(), "Choose a color", Color.black);
176                if (newColor != null) {
177                    colorList.add(index,newColor);
178                    colorList.remove(index+1);
179                    procListModel.removeAllElements();
180    
181                    for (int i=0; i<setProcessFiles.size(); i++) {
182                        if (colorList.get(i) == null)
183                            procListModel.addElement(setProcessFiles.get(i));
184                        else
185                            procListModel.addElement("<html><font color=#" + getColorHex((Color)colorList.get(i)) + ">"
186                                                 + setProcessFiles.get(i) +" </font></html>" );
187                    }
188                }
189            }
190        }
191    
192        private void onOK() {
193    
194            if (procListModel.size() < 2) {
195               Scene.showErrorMessage("Please select at least 2 data source.");
196               return;
197            }
198    
199            if (txtConfName.getText().trim().length() == 0) {
200                Scene.showErrorMessage("No configuration file selected!");
201                return;
202            }
203            dialog.setVisible(false);
204            noEntityAvailable = false;
205    
206            overlappedVisualize();
207        }
208    
209        private void onCancel() {
210            dialog.setVisible(false);
211        }
212    
213        private void addFiles() {
214            int[] selectIndex = fileList.getSelectedIndices();
215            ArrayList newColorList = new ArrayList();
216    
217            for (int i=0;i<selectIndex.length;i++) {
218                if (setProcessFiles.contains(fileListModel.getElementAt(selectIndex[i]))) continue;
219    
220                setProcessFiles.add(fileListModel.getElementAt(selectIndex[i]));
221                procListModel.addElement(fileListModel.getElementAt(selectIndex[i]));
222            }
223    
224            for (int i=0; i<setProcessFiles.size();i++) {
225                if (i<colorList.size())
226                    newColorList.add(i,colorList.get(i));
227                else
228                    newColorList.add(i,null);
229            }
230            colorList = newColorList;
231        }
232    
233        private void removeFiles() {
234            int[] selectIndex = procList.getSelectedIndices();
235    
236            for (int i=selectIndex.length -1 ;i>=0;i--) {
237                setProcessFiles.remove(selectIndex[i]);
238                colorList.remove(selectIndex[i]);
239                procListModel.removeElement(procListModel.getElementAt(selectIndex[i]));
240            }
241        }
242    
243        private String chooseConfig() {
244            JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastConfigDir());
245    
246            if(fc.showOpenDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) {
247                File f = fc.getSelectedFile();
248                Scene.getUIManager().setLastConfigDir(f.getPath());
249                return (f.getPath());
250            }
251            else return "";
252        }
253    
254        private void fillFileList() {
255            JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastDataDir());
256            String path;
257    
258            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
259    
260            fileListModel.removeAllElements();
261            if(fc.showOpenDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) {
262                path = fc.getSelectedFile().getAbsolutePath();
263                Scene.getUIManager().setLastDataDir(path);
264                File dir = new File(path);
265                FileSystemView fv = fc.getFileSystemView();
266                File[] fl = fv.getFiles(dir,false);
267                for (int i=0;i<fl.length;i++) {
268                    String fn = fl[i].getName();
269                    if (!fl[i].isFile() || !fn.endsWith(".dat") || setFile.contains(path+File.separator+fn))
270                        continue;
271                    setFile.add(path+File.separator+fn);
272                    fileListModel.addElement(/*path+File.separator+*/fn);
273                }
274            }
275    
276        }
277    
278        public boolean isOverlapable(Visualization visualToBeOverlapped) {
279            return true;
280        }
281    
282        public void preOverlappedVisualize() throws EVolveException{
283            String dataSource;
284            VizInfo vizInfo = null;
285            HashMap mapConfig = new HashMap();
286            Visualization visual;
287    
288            reset();
289            for (int i=0; i<procListModel.size(); i++) {
290                dataSource = (String)setProcessFiles.get(i);
291                Scene.setDataFilename(dataSource);
292                Scene.autoLoadDataSource();
293    
294                if (i == 0) {
295                    SerializerInfo info = VisualizationSerializer.v().getVizInfoFromDisk(txtConfName.getText().trim());
296                    vizInfo = (VizInfo)info.vizInfoList.get(0);
297                }
298    
299                mapConfig.put("Factory",vizInfo.getFactory());
300                mapConfig.put("Subject",vizInfo.getSubject());
301                mapConfig.put("Dimension",vizInfo.getDimension());
302                mapConfig.put("Predictor",vizInfo.getPredictor());
303                mapConfig.put("Interval",new Integer(vizInfo.getInterval()));
304                mapConfig.put("BeginCall",new Long(vizInfo.getBeginCall()));
305                mapConfig.put("EndCall",new Long(vizInfo.getEndCall()));
306    
307                visual = Scene.getVisualizationManager().newVisualization(((VisualizationFactory)mapConfig.get("Factory")).getName());
308    
309                visual.setName(vizInfo.getTitle());
310                visual.autoUpdateConfiguration(mapConfig);
311                visualizationList.add(visual);
312    
313                Scene.getVisualizationManager().addAllVisualizations();
314                Scene.getVisualizationManager().prepareListToBeVisualized();
315                Scene.autoVisualize();
316    
317                for (int k=0; k< entityList.length; k++)
318                    if (vizInfo.getDimension()[k] instanceof ReferenceDimension) {
319                        entityList[k].add(Scene.getDataManager().getEntity()[vizInfo.getDimension()[k].getDataFilter().getTargetType()]);
320                        referenceDimList[k].add(visual.getDimension()[k]);
321                    }
322            }
323    
324        }
325    
326        public void overlappedVisualize() {
327            Visualization visual;
328    
329            Scene.getVisualizationManager().init();
330            Scene.getUIManager().init();
331    
332            if (window instanceof EVolve.Window) {
333                Scene.getUIManager().removeWindow(window);
334                window = null;
335            }
336            visualizationList.clear();
337            Scene.getUIManager().setDoNotRemoveWindow(true);
338            try {
339                preOverlappedVisualize();
340            } catch (EVolveException e) {
341                Scene.showErrorMessage(e.getMessage());
342                Scene.getUIManager().setDoNotRemoveWindow(false);
343                Scene.setDataFilename(null);
344                return;
345            }
346            Scene.getUIManager().setDoNotRemoveWindow(false);
347            Scene.setDataFilename(null);
348    
349    
350            unifyVisualizations();
351    
352            // begin drawing overlap
353            if (window instanceof EVolve.Window) {
354                Scene.getUIManager().removeWindow(window);
355                window = null;
356            }
357    
358            newOverlappedVisualization(this);
359    
360            int [] selectedIndex = new int[2];
361            int [] sortedDimension = new int[2];
362            visual = (Visualization)visualizationList.get(0);
363    
364            for (int i=0; i<fullEntitySet.length; i++) {
365                if (visual.getDimension()[i] instanceof ReferenceDimension) {
366    
367                    selectedIndex[i] = ((ReferenceDimension)visual.getDimension()[i]).getSelectedComparatorIndex();
368                    sortedDimension[i] = 1;
369                    break;
370                }
371            }
372    
373            sort();
374            enableSortMenu();
375        }
376    
377        private void unifyVisualizations() {
378            // now we begin unify all entities
379            HashMap standard[] = new HashMap[2];
380            standard[0] = new HashMap();
381            standard[1] = new HashMap();
382    
383            for (int i=0; i<entityList.length; i++) {
384                for (int j=0; j<entityList[i].size(); j++) {
385                    Unification.unifyEntities(standard[i],(HashMap)entityList[i].get(j));
386                }
387            }
388    
389            // dump the unified entities into a hashset
390            for (int i=0; i<standard.length; i++) {
391                Iterator it = standard[i].keySet().iterator();
392                fullEntitySet[i] = new HashSet();
393                while (it.hasNext()) {
394                    fullEntitySet[i].add(standard[i].get(it.next()));
395                }
396            }
397            // unifying entities done here
398    
399            Visualization visual;
400            for (int i=0; i<entityList.length; i++) {
401                if (entityList[i].size() == 0) continue; // not a reference dimension
402                for (int j=0; j<visualizationList.size(); j++) {
403                    visual = (Visualization)visualizationList.get(j);
404    
405                    ReferenceDimension dim = visual.getLinkableDimension(i);
406                    Color newColor = (Color)colorList.get(j);
407                    Unification.changeColor(visual.getImage(),newColor);
408    
409                    dim.linkEntities(fullEntitySet[i]);
410                    try {
411                        dim.visualize();
412                    } catch (NoDataPlotException e) {
413                        Scene.showErrorMessage(e.getMessage());
414                    }
415                    ((XYVisualization)visual).disablePopupMenu();
416                }
417            }
418        }
419    
420        private void reset() {
421            fullEntitySet = new HashSet[2];
422            fullEntitySet[0] = null;
423            fullEntitySet[1] = null;
424            for (int i= 0; i<entityList.length ; i++) {
425                entityList[i].clear();
426            }
427        }
428    }